<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 值班记录管理
 * @package app\position
 * @author LJQ<245369209@qq.com>
 */
class Position  extends Api

{

    /**
   *  值班记录列表
   * @param string 
   * @return Json
   */

	public function positionList($positionId=null){
             $where= [] ;
             if ($positionId) {
             $where['id']=['eq',$positionId]; 
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 值班记录列表
			$info =collection(model('duty/position')->with('staff,enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//值班记录列表
			$info =collection(model('duty/position')->with('staff,enterprise')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('duty/position')->where($where)->count('id'); 
		
    		      if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到值班记录','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'值班记录','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加值班记录
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('duty/position');
          $result = $model->change($data);
          if (false != $result) {
             setlog('positionupdate','position',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'值班记录添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'值班记录添加失败,请检查']);
          }
      }

   	

   /**
   * 修改值班记录
   * @param  string positionId 值班记录id,
   * @return Json
   */
   	public function edit($data,$positionId){
        
          if (empty($positionId)) {
              return json(['code'=>0,'msg'=>'值班记录不存在,请检查']);
          }
          $model = model('duty/position');     
          if ($model->change($data)) {
            setlog('positiondel','position',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'值班记录修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'值班记录修改失败,请检查']);
            }
    }
    /**
   *  删除值班记录
   * @param string positionId 值班记录id,
   * @param Json
   */
     
    public function del($positionId) {
        $positionId = input('positionId',0,'intval');
        $model = model('duty/position');
      	if (empty ( $positionId )||empty(($model->where(array('id'=>$positionId))->find()))) {
           	return  json(['code'=>0,'msg'=>'值班记录id不存在,请检查']);
        }
        if($model->destroy(['id'=>$positionId]) !== FALSE){
        	 return json(['code'=>1,'msg'=>'值班记录删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'值班记录删除失败,请检查']);
       }
    }

     
   /**
   *   查询值班记录
   * @param keyword值班记录名称
   * @return Json
   */
   	public function search($start=null,$end=null,$enterpriseId=null){
   		$where = [] ;

		if($enterpriseId){
			
			$where['enterpriseId'] = ['eq',$enterpriseId];
		}
            if($start&&$end){
                 $start=strtotime($start);
                 $end=strtotime($end);

                 $where['createTime'] = ['between time',[$start,$end]];
            }

   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//值班记录列表
			$info =collection(model('position')->with('staff,enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//值班记录列表
			$info =collection(model('position')->with('staff,enterprise')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('position')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到值班记录','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'值班记录','count'=>$count,'data'=>$info]);
       	}
      }
}